package com.emar.adx.yiqifa.controller;

import com.emar.adx.yiqifa.util.ConfigurationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by xiaoliang on 2015/8/6 16:34.
 */
@Controller
public class HomeController {

    private static final Logger logger2 = LoggerFactory.getLogger("com.emar.adx.yiqifa.controller.HomeController");

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    private static final int connectTimeout = ConfigurationFactory.getConfiguration("config.properties").getInt("jd.connectTimeout");
    private static final int readTimeout = ConfigurationFactory.getConfiguration("config.properties").getInt("jd.readTimeout");

    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String home(Locale locale, Model model, HttpServletRequest request) {
        logger.debug("index...");
        return "index";
    }

    @RequestMapping(value = "/test", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String test(Locale locale, Model model, HttpServletRequest request) {
        logger.debug("test...");
        logger.debug("logout");
        logger.debug("debug");
        logger.info("info");
        logger.warn("warn");
        logger.error("error");


        logger2.debug("logout");
        logger2.debug("TestB-debug");
        logger2.info("TestB-info");
        logger2.warn("TestB-warn");
        logger2.error("TestB-error");

        return "test";
    }

    @RequestMapping(value = "/phone", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String phone(Locale locale, Model model, HttpServletRequest request) {
        logger.debug("test...");
        return "phone";
    }

    private Map<String, Object> initJd() {

        Map<String, Object> back = new HashMap<String, Object>();

        Map<String, Object> jingdong_service_promotion_getcode_responce = new HashMap<String, Object>();
        jingdong_service_promotion_getcode_responce.put("code", "0");

        Map<String, Object> queryjs_result = new HashMap<String, Object>();
        queryjs_result.put("resultCode", "0");
        queryjs_result.put("resultMessage", "success!!!");
        queryjs_result.put("url", "http://union.click.jd.com/jdc?e=&p=AyIBZRprFDJWWA1FBCVbV0IUEEULRFRBSkAOClBMW0sFa0JSW18KGARvYGZxFmApbl9aX09IH0MOHjdSHl0VABQAVitZEgAUDmUeUhQDEzc0aWtebBM3ZRteEAMQD10eWxwDGg9lHA%3D%3D&t=W1dCFBBFC0RUQUpADgpQTFtLBQ%3D%3D");
        jingdong_service_promotion_getcode_responce.put("queryjs_result", queryjs_result);

        back.put("jingdong_service_promotion_getcode_responce", jingdong_service_promotion_getcode_responce);
        return back;
    }

    @RequestMapping(value = "/routerjson")
    @ResponseBody
    public Object routerjson(Locale locale, Model model, HttpServletRequest request) {

        try {
            Thread.sleep(connectTimeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        String promotionType = request.getParameter("promotionType");

        Map<String, Object> map = new HashMap<String, Object>();
        if ("7".equals(promotionType)) {
            map = this.initJd();
        } else {
            map = this.initJd();
        }

        return map;
    }


}

